package com.atguigu.msnservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msnservice.service.MsmService;
import com.atguigu.msnservice.utils.ConstantPropertiesUtils;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author zhaolie
 * @Date 2020/12/17 4:39 下午
 * @Version 1.0
 * @Description
 */

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean send(Map<String, Object> param, String phone) {
        String accessKeyid = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessSecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyid, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");
        request.putQueryParameter("TemplateCode", "SMS_207135760");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(param);
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
